package com.hikeen.launcherx.functions.wifi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;

import com.hikeen.launcherx.MyApplication;


public class WifiBroadCastReceiver extends BroadcastReceiver {
    private String TAG = "WifiBroadCastReceiver";
    private static OnWIFIStateListener mOnWIFIStateListener;
    private WifiManager mWifiManager = (WifiManager) MyApplication.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    private AccessPoint accessPoint = AccessPoint.getAccessInstance();
    private Context context;


    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        switch (intent.getAction()) {
            case WifiManager.WIFI_STATE_CHANGED_ACTION:  //WIFI开关变化通知
                Log.d(TAG, "onReceive: -- 1");
                int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                        WifiManager.WIFI_STATE_DISABLED);
                switch (wifiState) {
                    case WifiManager.WIFI_STATE_DISABLED: //WIFI关闭
                        Log.d(TAG, "WIFI关闭");
                        mOnWIFIStateListener.onStateDisable();
                        break;
                    case WifiManager.WIFI_STATE_ENABLED:  //WIFI开启
                        Log.d(TAG, "WIFI开启");
                        mOnWIFIStateListener.onStateEnable();
                        break;
                }
                break;
            case WifiManager.SCAN_RESULTS_AVAILABLE_ACTION:  //WIFI扫描结果通知
                Log.d(TAG, "WIFI扫描结果通知");
                mOnWIFIStateListener.wifiAvailAble();
                break;
            case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:  //WIFI连接结果通知
                Log.d(TAG, "WIFI连接结果通知");
                mOnWIFIStateListener.wifiChangedAble();
                break;
            case WifiManager.NETWORK_STATE_CHANGED_ACTION:  //网络状态变化通知
                Log.d(TAG, "网络状态变化通知");
                boolean resultNoConnecty = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                Log.d(TAG, "onReceive: resultNoConnecty  " + resultNoConnecty);
                mOnWIFIStateListener.networkChangedAble(true);
                if (!resultNoConnecty) {
                    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                    info.getState();// 可获取当前NetworkInfo 的 status 信息;
                    info.getType();// 返回当前NetworkInfo 的 连接类型, WiFi, 蓝牙, 以太网 ...
                    Log.d(TAG, "onReceive: success---state:" + info.getState() + "----type: " + info.getType() + "----extrainfo:" + info.getExtraInfo()
                            + "----reason: " + info.getReason() + "----subtypename:" + info.getSubtypeName() + "----subtype:" + info.getSubtype() + "----typename:" + info.getTypeName());
                    accessPoint.setEncryptionType(info.getType() + "");
                    accessPoint.setSsid(getConnectWifiSsid() + "");
                    WifiInfo info2 = mWifiManager.getConnectionInfo();
//                    Message message = Message.obtain();
//                    message.what = 0x112;
//                    message.obj = WIFIUtils.getWIFISSID(context);
//                    GlobalApplication.getWindowService().mHandler.sendMessage(message);
                } else {
                    Log.d(TAG, "onReceive: error 当前无网络连接");
                    mOnWIFIStateListener.networkChangedAble(false);
//                    accessPoint.setSsid(context.getString(R.string.app_item_wirelessNetwork));
//                    Message message = Message.obtain();
//                    message.what = 0x112;
//                    message.obj =  context.getString(R.string.app_item_wirelessNetwork);
//                    GlobalApplication.getWindowService().mHandler.sendMessage(message);

                }
                break;

            case ConnectivityManager.CONNECTIVITY_ACTION:
                Log.d(TAG, "onReceive:  网络连接发生了变化");
                resultNoConnecty = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                Log.d(TAG, "onReceive: resultNoConnecty  " + resultNoConnecty);
                if (!resultNoConnecty) {
                    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                    info.getState();// 可获取当前NetworkInfo 的 status 信息;
                    info.getType();// 返回当前NetworkInfo 的 连接类型, WiFi, 蓝牙, 以太网 ...
                    Log.d(TAG, "onReceive: success---state:" + info.getState() + "----type: " + info.getType() + "----extrainfo:" + info.getExtraInfo()
                            + "----reason: " + info.getReason() + "----subtypename:" + info.getSubtypeName() + "----subtype:" + info.getSubtype() + "----typename:" + info.getTypeName());
//                    Message message = Message.obtain();
//                    message.what = 0x112;
//                    message.obj = getConnectWifiSsid();
//                    GlobalApplication.getWindowService().mHandler.sendMessage(message);
                } else {
                    Log.d(TAG, "onReceive: 当前无网络连接");
//                    accessPoint.setSsid("当前无网络连接！");
//                    Message message = Message.obtain();
//                    message.what = 0x112;
//                    message.obj = getConnectWifiSsid();
//                    GlobalApplication.getWindowService().mHandler.sendMessage(message);
                }

                break;
            default:
                break;
        }
    }

    public interface OnWIFIStateListener {
        void onStateDisable();

        void onStateEnable();

        void wifiAvailAble();

        void wifiChangedAble();

        void networkChangedAble(boolean f);
    }

    public   void setOnWiFiStateListener(OnWIFIStateListener onWiFiStateListener) {
         mOnWIFIStateListener = onWiFiStateListener;
    }

    private String getConnectWifiSsid() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            ConnectivityManager cm = (ConnectivityManager) MyApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            assert cm != null;
            NetworkInfo info = cm.getActiveNetworkInfo();
            if (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI) {
//                return info.getExtraInfo();
//                return context.getString(R.string.wifi_connected);
                return  null;
            }
        } else {
            WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
            Log.d("wifiInfo", wifiInfo.toString());
            Log.d("SSID", wifiInfo.getSSID());
//            return wifiInfo.getSSID();
//            return context.getString(R.string.wifi_connected);
            return null;
        }
//        return context.getString(R.string.wifi_disconnected);
        return null;
    }


}
